from fastai2.vision.all import *
from fastai2.vision.widgets import *
from utils import *
import shutil
key = 'bfe4c5c0c789435b9894b166b1ef6d6a'
path = Path('data'); path.ls()
def search_images_bing(key, term, count, min_sz=128):
client = api('https://api.cognitive.microsoft.com', auth(key))
return L(client.images.search(query=term, count=count, min_height=min_sz, min_width=min_sz).value)
labels = ['poison-oak', 'leaves']
counts = [150, 450]
for label, count in zip(labels, counts):
os.makedirs(path/label, exist_ok=True)
res = search_images_bing(key, label, count)
download_images(path/label, urls=res.attrgot('content_url'))
(path/labels[0]).ls(), (path/labels[1]).ls()
verify_images(get_image_files(path))[-5:]
bad = verify_images(get_image_files(path))
print(bad)
for img in bad: img.unlink()
(path/'poison-oak').ls(), (path/'leaves').ls()
data = DataBlock(blocks=(ImageBlock, CategoryBlock),
get_items=get_image_files,
get_y=parent_label,
splitter=RandomSplitter(valid_pct=0.2, seed=42))
aug_data = data.new(item_tfms=RandomResizedCrop(224), batch_tfms=aug_transforms())
dls = aug_data.dataloaders(path, bs=64)
dls.show_batch(max_n=5, nrows=1, unique=True)
xb, yb = first(dls.valid)
xb.shape, yb.shape
dls.show_batch(max_n=9, figsize=(9, 9))
len(dls.train_ds), len(dls.valid_ds)
learn = cnn_learner(dls, resnet34, metrics=accuracy)
learn.fine_tune(4)
learn.fine_tune(4)
interp = ClassificationInterpretation.from_learner(learn)
learn.show_results(max_n=9, figsize=(9, 9))
interp.plot_confusion_matrix()
interp.plot_top_losses(5, figsize=(15, 15))
cleaner = ImageClassifierCleaner(learn, height=256)
cleaner
Make sure to delete/relabel images before moving onto next class / dataset. Image cleaner's tracking resets!
cleaner.delete()
PILImage.create(cleaner.fns[3]).show(figsize=(15, 15))
for i in cleaner.delete(): cleaner.fns[i].unlink()
cleaner.change(), cleaner.fns[14]
for i, new_lbl in cleaner.change(): shutil.move(str(cleaner.fns[i]), path/new_lbl/'relabeled.jpg')
img = PILImage.create('poison-oak.jpg')
img.to_thumb(440)
learn.predict(img)